// 校验登录状态
function checkLogin() {
    var info = localStorage.getItem('userInfo')

    if (!info || info === '') {
        window.location.href = './login.html'
        return false
    }

    return true
}

// 校验已登录
function checkLogin2() {
    var info = localStorage.getItem('userInfo')

    if (info) {
        window.location.href = './index.html'
        return false
    }

    return true
}

// 获取登录信息
function getLoginInfo() {
    let info = localStorage.getItem('userInfo')

    if (!info || info === '' || info === '{}') {
        return false
    }

    return JSON.parse(info)
}

// 获取登录信息
function getInfo() {
    var yidenglu = document.getElementById('yidenglu')
    var weidenglu = document.getElementById('weidenglu')
    var loginState = document.getElementById('loginState')
    var nickName = document.getElementById('nickName')
    var articleTotal = document.getElementById('articleTotal')
    var publishTotal = document.getElementById('publishTotal')
    var commentTotal = document.getElementById('commentTotal')
    var likeTotal = document.getElementById('likeTotal')
    var spitTotal = document.getElementById('spitTotal')
    var userInfo = getLoginInfo()

    if (checkLogin()) {
        yidenglu.style.display = 'block'
        weidenglu.style.display = 'none'
        yidenglu.innerHTML = `<span>${userInfo.username}</span><span style="margin-left: 10px; color: #6a798e;" onclick="handleExit()">退出账号</span>`
        loginState.innerText = userInfo.isLogin ? '已登录' : '未登录'
        nickName.innerText = userInfo.nickName
        articleTotal.innerText = userInfo.a
        publishTotal.innerText = userInfo.b
        commentTotal.innerText = userInfo.c
        likeTotal.innerText = userInfo.d
        spitTotal.innerText = userInfo.e
    } else {
        yidenglu.style.display = 'none'
        weidenglu.style.display = 'block'
    }
}

function handleExit() {
    localStorage.removeItem('userInfo')
    window.location.href = `./login.html`
}